// pages/interview_take/interview_take.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator'),chatIM = require('../../utils/websocket.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    today: util.formatDatetime(new Date(),'yyyy-mm-dd'),
    now: util.formatDatetime(new Date(),'hh:ii'),
    jobs: [],
    apply: null,
    interview: {},
    job: null,
    sendMessage: 1, // 发消息开关

    date_show:true,
    check:false,
    // notices: ['请带简历','请着正装','请带作品','请带身份证'],
    selectedNoticeIndexes:[false,false,false,false],
    multi_array: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'], ['00', '30']],
    multi_index: [10, 0],
  },
  bindMultiPickerChange:function(e){
    let value = e.detail.value;
    let arr = this.data.multi_array;
    let n_time = arr[0][value[0]] + ":" + arr[1][value[1]];
    this.setData({
      multi_index:value,
      time:n_time
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
    });
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value,
    });
  },
  bindSendMessageChange:function(e){
    this.setData({
      sendMessage: !this.data.sendMessage ? 1:0
    })
  },
  choose:function(e){
    var arr=this.data.selectedNoticeIndexes
    var index=e.currentTarget.dataset.index
    arr[index] = !arr[index]
    this.setData({
      arr: arr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('dataTransfer', data => {
      this.setData({resume:data.resume, apply: data.apply||null, job: data.apply?data.apply.job:''});
      if(!data.apply) {
        //需要选择职位， 拉取可选择职位
        core.get({url:'xilujob.job_manage/index',data:{status:1},success:ret=>{this.setData({jobs:ret.data.jobs})}});
      }
    });
    //获得公司名称
    // let userinfo = app.getUserinfo(), url;
    // if(userinfo.enterprise_group_id==2) {
    //   url = 'summary/company';
    // }else {
    //   url = 'summary/labour';
    // }
    core.get({url:'xilujob.enterprise/info',success:ret=>{
      this.setData({
        companyName:ret.data.enterprise_info.company_name,
        companyAddress: ret.data.enterprise_info.address,
        contactPerson: ret.data.enterprise_info.contact_person,
        contactPhone: ret.data.enterprise_info.contact_phone
      });
    }});
  },

  bindJobChange(event) {
    let index = event.detail.value;
    this.setData({job: this.data.jobs[index]});
  },

  bindSubmit(event) {
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        date: {required:true},
        time: {required:true},
        job_id: {required:true},
        address: {required:true},
        contact: {required:true},
        contact_phone: {required:true,mobile:true},
        // contact_email: {email:true},
      },
      messages: {
        date: {required:'请选择面试日期'},
        time: {required:'请选择面试时间'},
        job_id: {required:'请选择面试职位'},
        address: {required:'请填写面试地址'},
        contact: {required:'请填写面试联系人'},
        contact_phone: {required:'请填写联系电话',mobile:'请填写正确的手机号'},
        // contact_email: {email:'请填写正确的邮箱'},
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    data.interview_time = data.date+' '+data.time;
    core.post({
      url: 'xilujob.job_interview/create_interview', data:data,
      success: ret => {
        let interview = ret.data.interview;
        wx.showModal({
          title: '提示',
          content: '面试邀请已发出',
          showCancel: false,
          success: res=>{
            chatIM.sendMessage(this.data.resume.user_id, interview.id,6);
			this.getOpenerEventChannel().emit('dataTransfer', {interview: interview});
            wx.navigateBack();
          }
        });

      }
    });
  }

});
